<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace pam\Mapper;

/**
 * Description of AgeingPolicyIndicatorsDataAccess
 *
 * @author robert.bwana
 */
use pam\Mapper\IndicatorsDataAccessInterface;
use Zend\Db\Adapter\AdapterInterface;
use pam\Model\Indicator;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Update;
use Zend\Db\Sql\Delete;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Stdlib\Hydrator\HydratorInterface;

class AgeingPolicyIndicatorsDataAccess implements IndicatorsDataAccessInterface {

    protected $dbAdapter;
    protected $hydrator;
    protected $indicator;
    protected $table = 'pam_AgeingIndicators';

    public function __construct(AdapterInterface $dbAdapter, HydratorInterface $hydrator, Indicator $indicator) {
        $this->dbAdapter = $dbAdapter;
        $this->hydrator = $hydrator;
        $this->indicator = $indicator;
    }

    public function delete($id) {
        $action = new Delete($this->table);
        $action->where(array(
            'id = ?' => $id
        ));

        $sql = new Sql($this->dbAdapter);
        $stmt = $sql->prepareStatementForSqlObject($action);
        $result = $stmt->execute();

        return (bool) $result->getAffectedRows();
    }

    /*
     * returns Indicator
     */

    public function find($id) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select($this->table);
        $select->where(array('id = ?' => $id));

        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();

        if ($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {
            return $this->hydrator->hydrate($result->current(), $this->indicator);
        } else {
            throw new \InvalidArgumentException("Indicator with given ID:{$id} not found.");
        }
    }

    public function findAllInPolicy($policyId) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select($this->table);
        $select->where(array('policy_id = ?' => $policyId));

        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();

        if ($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {

            $resultSet = new HydratingResultSet($this->hydrator, $this->indicator);
            //\Zend\Debug\Debug::dump($resultSet);
            return $resultSet->initialize($result);
            //return $this->hydrator->hydrate($result, $this->hydrator);
            //return array();
        } else {
            //throw new \InvalidArgumentException("Policy with given ID:{$policyId} not found.");
            return array();
        }
    }

    public function save(Indicator $indicator) {
        $indicatorData = $this->hydrator->extract($indicator);
        unset($indicatorData['id']);

        if ($indicator->getId()) {
            $action = new Update($this->table);
            $action->set($indicatorData);
            $action->where(array('id = ?' => $indicator->getId()));
        } else {
            $action = new Insert($this->table);
            $action->values($indicatorData);
        }

        $sql = new Sql($this->dbAdapter);
        $stmt = $sql->prepareStatementForSqlObject($action);
        $result = $stmt->execute();

        if ($result instanceof ResultInterface) {
            if ((bool) $newId = $result->getGeneratedValue()) {
                $indicator->setId($newId);
            }

            return $indicator;
        }
    }

}
